<template>
  <div v-show="store.state.app.isCollapse">
    <el-menu
      background-color="#001529"
      text-color="#CCC"
      active-text-color="#FFF"
      :default-active="store.state.app.activePath"
      class="el-menu-vertical-demo"
      :uniqueOpened="true"
      @open="handleOpen"
      @close="handleClose"
      @select="handleSelct"
    >
      <div v-for="(item, index) in menus" :key="index">
        <div
          class="px-6 text-gray-600 h-14"
          style="line-height: 3.5rem; font-size: 0.9rem"
        >
          <span>{{ index }}</span>
        </div>
        <div v-for="menu in item" :key="menu.path">
          <el-menu-item v-if="!menu.children" :index="menu.path">
            <i class="el-icon-menu"></i>
            <template #title>{{ menu.meta.title }}</template>
          </el-menu-item>
          <el-sub-menu v-else :index="menu.path">
            <template #title>
              <i class="el-icon-location"></i>
              <span>{{ menu.meta.title }}</span>
            </template>
            <el-menu-item
              v-for="child in menu.children"
              :index="child.path"
              :key="child.path"
            >
              {{ child.meta.title }}
            </el-menu-item>
          </el-sub-menu>
        </div>
      </div>
    </el-menu>
  </div>
</template>
<script setup>
import { vocationalWorkRouter } from "@/router/router";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import arrayToTree from "array-to-tree";
import { moreDimensionalToOneDimensionalArray } from "@/utils";
const router = useRouter();
const store = useStore();
const { children } = vocationalWorkRouter[0];

let menus = {};

const showMenu = arrayToTree(
  moreDimensionalToOneDimensionalArray(children).filter((item) => !item.hidden),
  {
    parentProperty: "pid",
    customID: "id",
  }
);

showMenu.forEach((item) => {
  if (menus[item.group]) {
    menus[item.group].push(item);
  } else {
    menus[item.group] = [item];
  }
});

function handleOpen(v) {}
function handleClose(v) {}
function handleSelct(v) {
  store.dispatch("app/setActivePath", v);
  router.push(v);
}
</script>

<style>
.el-menu.el-menu--collapse.el-menu-vertical-demo,
.el-menu-vertical-demo:not(.el-menu--collapse) {
  min-height: calc(100vh - 3rem);
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
}
</style>
